package kmp;

import com.sun.jmx.remote.internal.ArrayQueue;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;
import java.util.concurrent.LinkedBlockingQueue;

public class leetCode71_simplifyPath {
    public String simplifyPath(String path) {
        if(path == null) return null;
        String[] paths = path.split("/");
        Deque<String> stack = new ArrayDeque<>();
        for (String s : paths) {
            if(s.equals("..")){
                if(!stack.isEmpty()) {
                    stack.pop();
                }
            } else if(s.length() > 0 && !s.equals(".")){
                stack.push(s);
            }
        }
        if(stack.isEmpty()){
            return "/";
        }
        StringBuilder stringBuilder = new StringBuilder();
        while (!stack.isEmpty()){
            String pop = stack.pollLast();
            stringBuilder.append("/");
            stringBuilder.append(pop);
        }

        return stringBuilder.toString();

    }
}
